"""
# -*- coding: utf-8 -*-
# @Time    : 2023/10/8 8:39
# @Author  : 王摇摆
# @FileName: train.py
# @Software: PyCharm
# @Blog    ：https://blog.csdn.net/weixin_44943389?type=blog
"""
import matplotlib.pyplot as plt
import pandas as pd
import tensorflow as tf
from model import create_cnn_model_with_channel_attention

'''
1. 读取数据集，进行数据预处理
'''
train_data = pd.read_csv('dataset/train.csv')
test_data = pd.read_csv('dataset/test.csv')

# 提取特征和目标变量
X_train = train_data.drop(['id', 'target'], axis=1).values
y_train = train_data['target'].values

# 调整输入形状
X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], 1)  # 在最后添加一个维度作为通道数

'''
2. 获取模型
'''

# 定义输入形状和类别数量
input_shape = (X_train.shape[1], 1)  # 输入形状需要根据特征数量来确定

# 创建深层卷积神经网络模型
with tf.device('/device:GPU:0'):  # 指定在第一个GPU上运行
    model = create_cnn_model_with_channel_attention(input_shape)

# 编译模型
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# 超参数设置:在这里你可以调整 epoch 数量和批量大小，以及其他超参数，以获得更好的性能
epochs = 10
batch_size = 32
validation_split = 0.2

# 训练模型
history = model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size, validation_split=validation_split)
print('模型已训练完成')

'''
3. 绘制模型训练过程的曲线图
'''
#
# # 绘制训练过程中的损失函数
# plt.figure(figsize=(10, 6))
# plt.plot(history.history['loss'], label='Training Loss')
# plt.plot(history.history['val_loss'], label='Validation Loss')
# plt.xlabel('Epochs')
# plt.ylabel('Loss')
# plt.title('Training and Validation Loss')
# plt.legend()
# plt.show()
#
# # 绘制训练过程中的准确率
# plt.figure(figsize=(10, 6))
# plt.plot(history.history['accuracy'], label='Training Accuracy')
# plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
# plt.xlabel('Epochs')
# plt.ylabel('Accuracy')
# plt.title('Training and Validation Accuracy')
# plt.legend()
# plt.show()
